using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Adge.Models;
using Adge.BLL;
using NHibernate.Expression;
using Adge.Common;

namespace Adge.Web.UI.Admin
{
    public partial class NewsCenter : System.Web.UI.Page
    {
        int CatID
        {
            get
            {
                int subId = 0;
                string objDataId = Request["catid"];
                if (!string.IsNullOrEmpty(objDataId))
                {
                    if (Utils.IsNumber(objDataId))
                    {
                        int.TryParse(objDataId, out subId);
                    }
                }

                return subId;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                lnkNewsEdit.NavigateUrl += Utils.COMMA + Request.QueryString;
                InitInfos(CatID);
            }
        }        

        private void InitInfos(int catId)
        {
            Adge.BLL.AdgeArBase<CategoryInfo> catlist = new Adge.BLL.AdgeArBase<CategoryInfo>();
            #region Bind News
            Adge.BLL.AdgeArBase<NewsInfo> newslist = new Adge.BLL.AdgeArBase<NewsInfo>();
            NewsInfo[] entities = new NewsInfo[] { };

            AdgeArBase<CategoryInfo> bllCat = new AdgeArBase<CategoryInfo>();
            Order[] ordersCat = new Order[] { };
            ICriterion[] criteriasCat;
            CategoryInfo catInfo;
            ICriterion[] criterias;
            if (catId > 0)
            {
                criteriasCat = new ICriterion[] { new EqExpression("CatId", catId) };
                CategoryInfo[] cats = bllCat.SlicedFindAll(0, 2, ordersCat, criteriasCat);
                catInfo = cats[0];
                criterias = new ICriterion[] { new EqExpression("AdgeInfoCategory", catInfo) };
            }
            else
            {
                criterias = new ICriterion[] { };
            }            
            
            entities = newslist.SlicedFindAll(0, 100, new Order[] { Order.Desc("PubDate") }, criterias);
            gvNewsCenter.DataSource = entities;
            gvNewsCenter.DataBind();
            #endregion
        }

        protected void gvNewsCenter_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvNewsCenter.PageIndex = e.NewPageIndex;
            InitInfos(CatID);            
        }

        protected void gvNewsCenter_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {           
            object key = gvNewsCenter.DataKeys[e.RowIndex].Value;
            Adge.BLL.AdgeArBase<NewsInfo> newslist = new Adge.BLL.AdgeArBase<NewsInfo>();
            newslist.Delete(key);
            //liter.Text = key.ToString();
            InitInfos(CatID);
        }
        
    }
}
